package com.example.tool.adapter;

import android.content.Context;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.CardView;
import android.view.View;

import com.example.tool.R;
import com.example.tool.adapter.base.CommonAdapter;
import com.example.tool.adapter.base.CommonViewHolder;

import java.util.List;

/**
 * Created Date 2019/8/25.
 *
 * @author liangkun
 * ClassDescription：控件放大
 */
public class WidgetAdapter extends CommonAdapter<String> {
    public WidgetAdapter(Context context, List<String> datas, int layoutId) {
        super(context, datas, layoutId);
    }

    private int cuurentPosition = -1;
    private CardView cardViewCurrent;

    @Override
    protected void onBindData(final CommonViewHolder holder, String s, final int position) {
        holder.setText(R.id.tv_item, s);
        if (cuurentPosition != -1) {
            if (cuurentPosition != position) {
                //普通的view
                ((CardView) holder.getView(R.id.card_view)).setCardBackgroundColor(mContext.getResources().getColor(R.color.color_transpare));
                ((CardView) holder.getView(R.id.card_view)).setCardElevation(0);
                ViewCompat.animate((holder.getView(R.id.card_view))).scaleX(1.0f).scaleY(1.0f).start();
            } else {
                ((CardView) holder.getView(R.id.card_view)).setCardBackgroundColor(mContext.getResources().getColor(R.color.color_white));
                ((CardView) holder.getView(R.id.card_view)).setCardElevation(mContext.getResources().getDimensionPixelOffset(R.dimen.sw_dp_8));
                ViewCompat.animate((holder.getView(R.id.card_view))).scaleX(1.07f).scaleY(1.07f).start();
                cardViewCurrent = holder.getView(R.id.card_view);
                cuurentPosition = position;
            }

        }

        holder.setOnClickListener(R.id.card_view, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cuurentPosition != -1) {
                    if (cuurentPosition != position && cardViewCurrent != null) {
                        cardViewCurrent.setCardBackgroundColor(mContext.getResources().getColor(R.color.color_transpare));
                        cardViewCurrent.setCardElevation(0);
                        ViewCompat.animate(cardViewCurrent).scaleX(1.0f).scaleY(1.0f).start();
                    } else {
                        //点击同一个
                    }
                    cuurentPosition = position;
                    cardViewCurrent = (CardView) holder.itemView;

                } else {
                    cuurentPosition = position;
                    cardViewCurrent = (CardView) holder.itemView;
                }
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(v, holder, position);
                }
            }
        });
    }
}
